package jblip.gui.components;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.BoxLayout;
import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import jblip.gui.IconLoader;

public class ConfigFrame extends JFrame implements ListSelectionListener {
  private static final long serialVersionUID = 1L;

  private static final String FRAME_TITLE = "JBlip: konfiguracja";

  private static final ImageIcon ICON_PROFILE;

  private static final ImageIcon ICON_SETTINGS;

  private static final ImageIcon ICON_SUBSCRIPTIONS;

  private static final String LABEL_PROFILE = "Profil";

  private static final String LABEL_SETTINGS = "Ustawienia";

  private static final String LABEL_SUBSCRIPTIONS = "Śledzenie";

  static {
    ICON_PROFILE = IconLoader.loadIcon("conf_profile.png");
    ICON_SETTINGS = IconLoader.loadIcon("conf_settings.png");
    ICON_SUBSCRIPTIONS = IconLoader.loadIcon("conf_subscriptions.png");
  }

  private JList selection_list;

  private JPanel cards_panel;

  public ConfigFrame() {
    super(FRAME_TITLE);
    initUI();
    this.pack();
    this.setVisible(true);
  }

  private void initUI() {
    final JPanel pane = new JPanel();
    pane.setLayout(new BoxLayout(pane, BoxLayout.LINE_AXIS));

    setIconImage(ChannelFrame.ICON_FRAME.getImage());

    final JLabel lab_settings =
        new JLabel(LABEL_SETTINGS, ICON_SETTINGS, SwingConstants.CENTER);
    final JLabel lab_profile =
        new JLabel(LABEL_PROFILE, ICON_PROFILE, SwingConstants.CENTER);
    final JLabel lab_subsriptions =
        new JLabel(LABEL_SUBSCRIPTIONS, ICON_SUBSCRIPTIONS,
            SwingConstants.CENTER);

    selection_list =
        new JList(new Object[] { lab_profile, lab_subsriptions, lab_settings });
    selection_list.setCellRenderer(new LabelCellRenderer());
    selection_list.setLayoutOrientation(JList.VERTICAL);

    this.add(pane);

    cards_panel = new JPanel(new CardLayout());

    final JPanel profile_panel = new JPanel();
    profile_panel.add(new JLabel("Profil"));

    final JPanel settings_panel = new JPanel();
    settings_panel.add(new JLabel("Ustawienia"));

    final JPanel subscriptions_panel = new JPanel();
    subscriptions_panel.add(new JLabel("Śledzenie"));

    cards_panel.add(profile_panel, LABEL_PROFILE);
    cards_panel.add(settings_panel, LABEL_SETTINGS);
    cards_panel.add(subscriptions_panel, LABEL_SUBSCRIPTIONS);

    selection_list.addListSelectionListener(this);
    selection_list.setSelectedIndex(0);

    pane.add(selection_list);
    pane.add(cards_panel);
  }

  @Override
  public void valueChanged(final ListSelectionEvent lse) {
    if (!selection_list.isSelectionEmpty()) {
      final JLabel selected = (JLabel) selection_list.getSelectedValue();
      final CardLayout cards_lay = (CardLayout) cards_panel.getLayout();
      cards_lay.show(cards_panel, selected.getText());
    }
  }
}

class LabelCellRenderer extends DefaultListCellRenderer {
  private static final long serialVersionUID = 1L;

  @Override
  public Component getListCellRendererComponent(JList list, Object value,
      int index, boolean isSelected, boolean cellHasFocus) {
    super.getListCellRendererComponent(
        list, value, index, isSelected, cellHasFocus);

    if (value instanceof JLabel) {
      final JLabel label = (JLabel) value;
      setText(label.getText());
      setIcon(label.getIcon());
    }

    if (isSelected) {
      setBackground(Color.LIGHT_GRAY);
      setForeground(Color.BLACK);
    }

    return this;
  }
}